{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0ffcf242",
   "metadata": {},
   "source": [
    "----\n",
    "* 记录人（学习人）：何明诗\n",
    "* 时间：2021-11-15\n",
    "* week12\n",
    "* 学习线上资源：[python官方文档](www.python.com)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aeb6b92a",
   "metadata": {},
   "source": [
    "## 本周知识点\n",
    "\n",
    "### 1.字典的一般方法\n",
    "\n",
    "> 1.字典的查询 查询key->value  \n",
    ">> 1.先看字典的key 是否存在 ：in 查询成员是否存在的重要方法  \n",
    "> 2.字典的修改  \n",
    "> 3.字典删除  \n",
    "> 4.字典的增加（dict_name[key]=value）  \n",
    "\n",
    "### 2.字典的进阶方法\n",
    "\n",
    "> 1.字典的初始化  \n",
    ">> 1.D.fromkeys()  \n",
    ">> 2.setdefault（）  \n",
    "\n",
    "### 3.字典的实践（一）课本\n",
    "\n",
    "### 4.字典的实践（二）课本\n",
    "\n",
    "### 5.字典的实践（三）课外"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba10f713",
   "metadata": {},
   "source": [
    "## 1.字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e237e6ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiHua'}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiHua\"\n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d0063a9",
   "metadata": {},
   "source": [
    ">  1.字典的查找\n",
    ">> 1.直接查找 dict_name[key] 当关键词key不存在时，有可能报错\n",
    ">> 2.get（）方法 D.get(k[.d])当关键词key不存在时，返回none不存在。不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a70ff54d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接查找 dict_name[key]\n",
    "stu_info[\"stu1801\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "255a926e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function get:\n",
      "\n",
      "get(key, default=None, /) method of builtins.dict instance\n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4e1b6449",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'stu1804'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-af87bde9363c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mstu_info\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"stu1804\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'stu1804'"
     ]
    }
   ],
   "source": [
    "stu_info[\"stu1804\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a781a0c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "081626e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(stu_info.get(\"stu1804\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1444d31",
   "metadata": {},
   "source": [
    "> 2.修改 & 新增\n",
    ">> 1.先查询 查看key是否存在 使用 in 查询成员  \n",
    ">> 2.如果key存在 修改：dict_name[key] = value  \n",
    ">> 3.如果key不存在 新增：dict_name[key] = value  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dba286a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 stu1804\n",
    "\"stu1804\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2c449afb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 stu1803\n",
    "\"stu1803\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "90880354",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果key存在 修改     \"stu1803\":\"LiHua\"-->\"stu1803\":\"LiuYu\"\n",
    "if \"stu1803\" in stu_info:\n",
    "    stu_info[\"stu1803\"] = \"LiuYu\"\n",
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62579aaa",
   "metadata": {},
   "source": [
    "> 3.字典的删除  \n",
    ">> 1.D.pop（）的一般操作如下：  \n",
    ">>> 1.先查询是否存在 in  \n",
    ">>> 2.在使用删除的方法  \n",
    ">> 2.D.popitem（） "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "77fbaaab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.dict instance\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    If key is not found, d is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "43bdd3bf",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "pop expected at least 1 argument, got 0",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-13-d1ada3d451b0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mstu_info\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 不正确，不同于列表（删除最后一个）\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: pop expected at least 1 argument, got 0"
     ]
    }
   ],
   "source": [
    "stu_info.pop() # 不正确，不同于列表（删除最后一个）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a6fadaa0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典删除的正确使用\n",
    "if \"stu1803\" in stu_info:\n",
    "    stu_info.pop(\"stu1803\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "51199d54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8e5a0e1d",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'stu1804'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-17-41955efe772d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mstu_info\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"stu1804\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m: 'stu1804'"
     ]
    }
   ],
   "source": [
    "# 如果不先查询key是否存在，有可能报错 KeyError: 'stu1804'\n",
    "stu_info.pop(\"stu1804\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e39eb775",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem() method of builtins.dict instance\n",
      "    Remove and return a (key, value) pair as a 2-tuple.\n",
      "    \n",
      "    Pairs are returned in LIFO (last-in, first-out) order.\n",
      "    Raises KeyError if the dict is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5a2b242a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1802', 'XiaoFeng')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "898bc178",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02901203",
   "metadata": {},
   "source": [
    "## 字典的实践（一）\n",
    "\n",
    "* 课本P112 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "825d8766",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "# -*- coding:utf-8 -*-\n",
    "# Author:Zhichao\n",
    " \n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    " \n",
    "# found[\"a\"]=0\n",
    "# found[\"e\"]=0\n",
    "# found[\"i\"]=0\n",
    "# found[\"o\"]=0\n",
    "# found[\"u\"]=0\n",
    "found = found.fromkeys(vowels,0)\n",
    " \n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    " \n",
    "# print(found.items())\n",
    "for k,v in found.items():\n",
    "    print(k,\"was found\",v,\"time(s)\")\n",
    "# print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0dfd812",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 目标 :查询 元音字母是否出现在word文本当中，并且统计出现的次数\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# 字典 初始化 的值\n",
    "# found[\"a\"]=0\n",
    "# found[\"e\"]=0\n",
    "# found[\"i\"]=0\n",
    "# found[\"o\"]=0\n",
    "# found[\"u\"]=0\n",
    "\n",
    "for i in vowels:\n",
    "    #　使用字典的新增　构建初始化字典\n",
    "    found[i] = 0 \n",
    "    \n",
    "# 循环用户输入的文本 ，出现一次元音字符，对应的元音字符+1 \n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1 \n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33367849",
   "metadata": {},
   "source": [
    "* 字典的进阶方法：初始化所有值fromkeys（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "85560eb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(found.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e6087005",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:ddfdssdd\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# 初始化 字典\n",
    "found = found.fromkeys(vowels,0)\n",
    "\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "found\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dd7d0d6",
   "metadata": {},
   "source": [
    "> 字典的进阶方法： 初始化特定的值 setdefault（）\n",
    "\n",
    ">> * 查询 key 是否出现在字典当中\n",
    ">>> 1.出现过，就当没看见，不做任何操作\n",
    ">>> 2.key未出现过，给定key及value （指定的初始化的值）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae6045c8",
   "metadata": {},
   "source": [
    "* 字典的进阶方法：初始化特定的值 setdefault（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "abc92d64",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'found' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-0bc0db48ae16>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhelp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfound\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetdefault\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'found' is not defined"
     ]
    }
   ],
   "source": [
    "help(found.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ce6af011",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = {\n",
    "    \"百度\":\"baidu.com\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "00db0c95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'baidu.com'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url.setdefault(\"百度\",\"https://baidu.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a9f58bd6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'百度': 'baidu.com'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c8cc77d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password = {\n",
    "    \"XiaoMing\":\"222333\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "eb1facc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'123456'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_password.setdefault(\"lihua\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a5214979",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'123456'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_password.setdefault(\"xiaohong\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4cf2fbd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'222333'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_password.setdefault(\"XiaoMing\",\"123456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8d3ac03e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'XiaoMing': '222333', 'lihua': '123456', 'xiaohong': '123456'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_password"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5909d3ea",
   "metadata": {},
   "source": [
    "## 字典的实践（二）\n",
    "* 课本P121\n",
    "\n",
    "... # -*- coding:utf-8 -*-\n",
    " vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    " word = input(\"Provide a word to search for vowels:\")\n",
    " found = {}\n",
    " \n",
    " for letter in word:\n",
    "     if letter in vowels:\n",
    "         found.setdefault(letter,0)  #如果是元音字母，就在字典found中初始化这个元音字母，值为0\n",
    "         found[letter] +=1\n",
    " for k,v in sorted(found.items()):\n",
    "     print(k,v)\n",
    "..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d9cefa57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hello zhichao\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'e': 1, 'o': 2, 'i': 1, 'a': 1}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 优化：实践（一）\n",
    "\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found.setdefault(i,0) # 特定情况下（字典中不存在该元素时，初始化其值）\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c50f6c04",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
